package com.datawin.taotaocheche.ui.actviity.user.presenter;

import android.text.TextUtils;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import com.datawin.taotaocheche.base.BaseBiz;
import com.datawin.taotaocheche.comm.MyApplication;
import com.datawin.taotaocheche.event.RechargeEvent;
import com.datawin.taotaocheche.ui.actviity.user.biz.CertificationBiz;
import com.datawin.taotaocheche.ui.actviity.user.contract.CertificationContract;
import com.datawin.taotaocheche.util.FileUtils;
import com.datawin.taotaocheche.util.ValidatorUtil;

import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by Administrator on 2017/10/21.
 */

public class CertificationPresenter implements CertificationContract.Presenter {

    CertificationContract.View view;
    CertificationBiz certificationBiz;


    @Inject
    public CertificationPresenter(CertificationContract.View view) {
        this.view = view;
    }


    @Override
    public void setBiz(BaseBiz biz) {
        certificationBiz = (CertificationBiz) biz;
    }

    @Override
    public void getCheck(final String realName, final String companyName, final String companyTel, final String cardNo, final String city, final List<String> photoInfos) {
        if (TextUtils.isEmpty(realName)) {
            view.showMsg("真实姓名没有填写");
            return;
        }
        if (TextUtils.isEmpty(companyName)) {
            view.showMsg("公司名字未填写");
            return;
        }

        if (TextUtils.isEmpty(cardNo)) {
            view.showMsg("个人身份证未填写");
            return;
        }
        if(!ValidatorUtil.isIDCard(cardNo)){
            view.showMsg("请输入正确18位或者15位身份证号码");
            return;
        }
        if (TextUtils.isEmpty(city)) {
            view.showMsg("所在城市未填写");
            return;
        }
        if (photoInfos.size() != 2) {
            view.showMsg("选择2张认证图片");
            return;
        }
        view.loading(true);
        Map<String, RequestBody> params = new HashMap<>();
        params.put("userCode", FileUtils.convertToRequestBody(MyApplication.userInfo.getUserCode()));
        params.put("realName", FileUtils.convertToRequestBody(realName));
        params.put("companyName", FileUtils.convertToRequestBody(companyName));
        params.put("companyTel", FileUtils.convertToRequestBody(companyTel));
        params.put("cardNo", FileUtils.convertToRequestBody(cardNo));
        params.put("city", FileUtils.convertToRequestBody(city));
        List<File> fileList = new ArrayList<>();
        for (int i = 0; i < photoInfos.size(); i++) {
            File file = new File(photoInfos.get(i));//filePath 图片地址
            fileList.add(file);
        }

        List<MultipartBody.Part> partList = FileUtils.filesToMultipartBodyParts(fileList);
        certificationBiz.setInfo(params, partList, new BaseBiz.Callback<Object>() {
            @Override
            public void onSuccess(Object o) {
                view.loading(false);
                EventBus.getDefault().post(new RechargeEvent());
                view.showMsg("申请认证成功");
                view.finishAct();
            }

            @Override
            public void onFailure(Object o) {
                view.showMsg("申请认证失败");
                view.loading(false);
            }
        });
    }


}
